home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / Dev / Oberon / source / FPE / StringDialog.mod < prev    next >
Text File  |  1995-06-29  |  9KB  |  341 lines

  1. (***************************************************************************
  2.  
  3.      $RCSfile: StringDialog.mod $
  4.   Description: Defines and implements a simple string dialog.
  5.  
  6.    Created by: fjc (Frank Copeland)
  7.     $Revision: 1.10 $
  8.       $Author: fjc $
  9.         $Date: 1995/06/04 22:42:36 $
  10.  
  11.   Copyright © 1994-1995, Frank Copeland.
  12.   This file is part of the Oberon-A Library.
  13.   See Oberon-A.doc for conditions of use and distribution.
  14.  
  15.   Log entries are at the end of the file.
  16.  
  17. ***************************************************************************)
  18.  
  19. <* STANDARD- *> <* MAIN- *> <*$ LongVars+ *> <*$ NilChk- *>
  20.  
  21. MODULE StringDialog;
  22.  
  23. IMPORT
  24.   SYS := SYSTEM, e := Exec, gfx := Graphics, i := Intuition,
  25.   iu := IntuiUtil, ev := Events, is := IntuiSup, ise := ISupEvents,
  26.   u := Util;
  27.  
  28. CONST
  29.  
  30.   NumGadgets = 3;
  31.   NumTexts   = 1;
  32.  
  33. TYPE
  34.  
  35.   StrDlg * = POINTER TO StrDlgRec;
  36.   StrDlgRec = RECORD (ise.ISupDialogRec)
  37.     g0         : is.InputData;
  38.     g1         : is.ButtonData;
  39.     g2         : is.ButtonData;
  40.     gDataEnd   : LONGINT;
  41.     t0         : is.TextData;
  42.     tDataEnd   : INTEGER;
  43.     textBuffer : ARRAY 256 OF CHAR;
  44.     result     : BOOLEAN;
  45.   END; (* StrDlgRec *)
  46.  
  47.   StrDlgPort = POINTER TO StrDlgPortRec;
  48.   StrDlgPortRec = RECORD (ise.ISupPortRec)
  49.     strDlg : StrDlg;
  50.   END;
  51.  
  52. CONST
  53.  
  54.   AcceptText = "_Accept";
  55.   CancelText = "_Cancel";
  56.  
  57.   StringGadgetID = 0;
  58.   AcceptButtonID = 1;
  59.   CancelButtonID = 2;
  60.  
  61. (* ----- Support procedures ----- *)
  62.  
  63.  
  64. (*------------------------------------*)
  65. PROCEDURE CalcTextBox
  66.   ( renderInfo : is.RenderInfoPtr;
  67.     text       : ARRAY OF CHAR;
  68.     font       : gfx.TextAttrPtr;
  69.     VAR width, height : INTEGER );
  70.  
  71. <*$CopyArrays-*>
  72. BEGIN (* CalcTextBox *)
  73.   IF font = NIL THEN font := SYS.ADR(renderInfo.textAttr) END;
  74.   iu.CalcTextBox (text, font, width, height);
  75. END CalcTextBox;
  76.  
  77.  
  78. (*------------------------------------*)
  79. PROCEDURE CalcTextButtonBox
  80.   ( renderInfo    : is.RenderInfoPtr;
  81.     text          : ARRAY OF CHAR;
  82.     font          : gfx.TextAttrPtr;
  83.   VAR width, height : INTEGER );
  84.  
  85.   CONST
  86.     extraWidth  = 10;
  87.     extraHeight =  6;
  88.   VAR
  89.     tempWidth, tempHeight : INTEGER;
  90.  
  91. <*$CopyArrays-*>
  92. BEGIN (* CalcTextButtonBox *)
  93.   CalcTextBox (renderInfo, text, font, tempWidth, tempHeight);
  94.   INC (tempWidth, extraWidth);
  95.   INC (tempHeight, extraHeight);
  96.   IF tempWidth > width THEN
  97.     width := tempWidth;
  98.   END;
  99.   IF tempHeight > height THEN
  100.     height := tempHeight;
  101.   END
  102. END CalcTextButtonBox;
  103.  
  104.  
  105. (*------------------------------------*)
  106. PROCEDURE CalcInputGadgetBox
  107.   ( renderInfo        : is.RenderInfoPtr;
  108.     visibleChars      : INTEGER;
  109.     VAR width, height : INTEGER );
  110.  
  111.   CONST
  112.     extraWidth  = 12;
  113.     extraHeight = 6;
  114.   VAR
  115.     tempWidth, tempHeight : INTEGER;
  116.  
  117. BEGIN (* CalcInputGadgetBox *)
  118.   CalcTextBox (renderInfo, "0", NIL, tempWidth, tempHeight);
  119.   tempWidth := tempWidth * visibleChars;
  120.   INC (tempWidth, extraWidth);
  121.   INC (tempHeight, extraHeight);
  122.   IF tempWidth > width THEN
  123.     width := tempWidth;
  124.   END;
  125.   IF tempHeight > height THEN
  126.     height := tempHeight;
  127.   END
  128. END CalcInputGadgetBox;
  129.  
  130.  
  131. (*------------------------------------*)
  132. PROCEDURE (sdp : StrDlgPort) HandleISup
  133.   (msg : i.IntuiMessagePtr) : INTEGER;
  134.  
  135.   VAR result : INTEGER; str : e.LSTRPTR;
  136.  
  137. BEGIN (* HandleISup *)
  138.   CASE msg.code OF
  139.     StringGadgetID :
  140.       str := msg.iAddress;
  141.       COPY (str^, sdp.strDlg.textBuffer);
  142.       result := ev.Continue
  143.     |
  144.     AcceptButtonID :
  145.       sdp.strDlg.result := (sdp.strDlg.textBuffer # "");
  146.       result := ev.Stop
  147.     |
  148.     CancelButtonID :
  149.       sdp.strDlg.result := FALSE;
  150.       result := ev.Stop
  151.     |
  152.   END;
  153.   is.ReplyMsg (msg);
  154.   RETURN result;
  155. END HandleISup;
  156.  
  157.  
  158. (*------------------------------------------------------------------------*)
  159. (* Exported procedures *)
  160.  
  161. (*------------------------------------*)
  162. PROCEDURE InitStrDlg *
  163.   ( dialog        : StrDlg;
  164.     renderInfo    : is.RenderInfoPtr;
  165.     title, prompt : ARRAY OF CHAR;
  166.     visibleChars,
  167.     maxChars      : INTEGER );
  168.  
  169.   CONST
  170.     HSpace = 8; VSpace = 4;
  171.  
  172.   VAR
  173.     textWidth, textHeight, stringWidth, stringHeight, buttonWidth,
  174.     buttonHeight, dialogWidth, dialogHeight
  175.       : INTEGER;
  176.     sdp : StrDlgPort;
  177.  
  178.   (*------------------------------------*)
  179.   PROCEDURE CalcStrDlg ();
  180.  
  181.   BEGIN (* CalcStrDlg *)
  182.     CalcTextBox (renderInfo, prompt, NIL, textWidth, textHeight);
  183.     stringWidth := 0; stringHeight := 0;
  184.     CalcInputGadgetBox (
  185.       renderInfo, visibleChars, stringWidth, stringHeight);
  186.     buttonWidth := 0; buttonHeight := 0;
  187.     CalcTextButtonBox (
  188.       renderInfo, AcceptText, NIL, buttonWidth, buttonHeight);
  189.     CalcTextButtonBox (
  190.       renderInfo, CancelText, NIL, buttonWidth, buttonHeight);
  191.     dialogWidth :=
  192.       u.MaxInt
  193.         ( u.MaxInt (textWidth, stringWidth), (buttonWidth * 2) + HSpace )
  194.       + (2 * HSpace);
  195.     dialogHeight := textHeight + stringHeight + buttonHeight + (4 * VSpace);
  196.   END CalcStrDlg;
  197.  
  198.   (*------------------------------------*)
  199.   PROCEDURE InitTexts (dialog : StrDlg);
  200.  
  201.   BEGIN (* InitTexts *)
  202.     dialog.t0.type     := is.text;
  203.     dialog.t0.flags    := {is.tdCenter};
  204.     dialog.t0.leftEdge := 0;
  205.     dialog.t0.topEdge  := VSpace;
  206.     dialog.t0.text     := SYS.ADR (prompt);
  207.     dialog.t0.textAttr := NIL;
  208.     dialog.tDataEnd    := is.dataEnd;
  209.   END InitTexts;
  210.  
  211.   (*------------------------------------*)
  212.   PROCEDURE InitGadgets (dialog : StrDlg);
  213.  
  214.     CONST
  215.       StringGadgetFlags = {is.gdMovePointer};
  216.       ButtonFlags = {is.gdHotKey};
  217.  
  218.     VAR halfWidth : INTEGER;
  219.  
  220.   BEGIN (* InitGadgets *)
  221.     dialog.g0.type := is.string;
  222.     dialog.g0.flags := StringGadgetFlags;
  223.     dialog.g0.leftEdge := (dialogWidth - stringWidth) DIV 2;
  224.     dialog.g0.topEdge := textHeight + (2 * VSpace);
  225.     dialog.g0.width := stringWidth;
  226.     dialog.g0.height := stringHeight;
  227.     dialog.g0.text := NIL;
  228.     dialog.g0.textAttr := NIL;
  229.     dialog.g0.len := maxChars;
  230.     dialog.g0.activateNext := 0;
  231.     dialog.g0.activatePrev := 0;
  232.     dialog.g0.default := NIL;
  233.  
  234.     halfWidth := dialogWidth DIV 2;
  235.  
  236.     dialog.g1.type := is.button;
  237.     dialog.g1.flags := ButtonFlags;
  238.     dialog.g1.leftEdge := (halfWidth - buttonWidth) DIV 2;
  239.     dialog.g1.topEdge := dialog.g0.topEdge + stringHeight + VSpace;
  240.     dialog.g1.width := buttonWidth;
  241.     dialog.g1.height := buttonHeight;
  242.     dialog.g1.text := SYS.ADR(AcceptText);
  243.     dialog.g1.textAttr := NIL;
  244.     dialog.g1.selected := 0;
  245.     dialog.g1.normalRender := NIL;
  246.     dialog.g1.selectRender := NIL;
  247.  
  248.     dialog.g2.type := is.button;
  249.     dialog.g2.flags := ButtonFlags;
  250.     dialog.g2.leftEdge := dialog.g1.leftEdge + halfWidth;
  251.     dialog.g2.topEdge := dialog.g1.topEdge;
  252.     dialog.g2.width := buttonWidth;
  253.     dialog.g2.height := buttonHeight;
  254.     dialog.g2.text := SYS.ADR(CancelText);
  255.     dialog.g2.textAttr := NIL;
  256.     dialog.g2.selected := 0;
  257.     dialog.g2.normalRender := NIL;
  258.     dialog.g2.selectRender := NIL;
  259.  
  260.     dialog.gDataEnd := is.dataEnd;
  261.   END InitGadgets;
  262.  
  263. <*$CopyArrays-*>
  264. BEGIN (* InitStrDlg *)
  265.   ASSERT (dialog # NIL, 137);
  266.   CalcStrDlg ();
  267.   dialog.reqData.title := SYS.ADR (title);
  268.   dialog.reqData.width := dialogWidth;
  269.   dialog.reqData.height := dialogHeight;
  270.   dialog.reqData.flags := {is.rdInnerWindow};
  271.   dialog.reqData.texts := SYS.ADR (dialog.t0);
  272.   dialog.reqData.gadgets := SYS.ADR (dialog.g0);
  273.   InitTexts (dialog);
  274.   InitGadgets (dialog);
  275.   dialog.result := FALSE;
  276.   NEW (sdp); ASSERT (sdp # NIL, 132);
  277.   sdp.strDlg := dialog; dialog.iSupPort := sdp
  278. END InitStrDlg;
  279.  
  280.  
  281. (*------------------------------------*)
  282. PROCEDURE Activate *
  283.   ( dialog     : StrDlg;
  284.     window     : i.WindowPtr;
  285.     VAR buffer : ARRAY OF CHAR )
  286.   : BOOLEAN;
  287.  
  288. BEGIN (* Activate *)
  289.   ASSERT (dialog # NIL, 137);
  290.   dialog.g0.default := SYS.ADR (buffer);
  291.   dialog.textBuffer := ""; dialog.result := FALSE;
  292.   IF ise.Activate (dialog, window) THEN
  293.     IF dialog.result THEN COPY (dialog.textBuffer, buffer) END
  294.   END;
  295.   RETURN dialog.result
  296. END Activate;
  297.  
  298. END StringDialog.
  299.  
  300. (***************************************************************************
  301.  
  302.   $Log: StringDialog.mod $
  303.   Revision 1.10  1995/06/04  22:42:36  fjc
  304.   *** empty log message ***
  305.  
  306.   Revision 1.9  1995/01/26  00:15:33  fjc
  307.   - Release 1.5
  308.  
  309.   Revision 1.8  1994/09/25  18:20:54  fjc
  310.   - Uses new syntax for external code declarations
  311.  
  312.   Revision 1.7  1994/08/08  16:13:41  fjc
  313.   Release 1.4
  314.  
  315.   Revision 1.6  1994/06/17  17:26:27  fjc
  316.   - Updated for release
  317.  
  318.   Revision 1.5  1994/06/09  13:42:30  fjc
  319.   - [bug] The text entered into the input gadget was in a
  320.     buffer dynamically allocated by IntuiSup.  This was being
  321.     freed before the text was copied.  Changed to copy the
  322.     text into the dialog object instead of just keeping a
  323.     pointer to it.
  324.  
  325.   Revision 1.4  1994/06/04  23:49:52  fjc
  326.   - Changed to use new Amiga interface
  327.  
  328.   Revision 1.3  1994/05/12  21:26:09  fjc
  329.   - Prepared for release
  330.  
  331.   Revision 1.2  1994/01/24  14:33:33  fjc
  332.   Changed to conform with changes in Module Handlers:
  333.     Handler procedures now reply to any messages they handle
  334.  
  335.   Revision 1.1  1994/01/15  17:32:38  fjc
  336.   Start of revision control
  337.  
  338. ***************************************************************************)
  339.  
  340.  
  341.